'use strict';

const xtpl = require('xtpl');
const path = require('path');
const captchapng = require('captchapng');
const db = require(path.join(__dirname,'../config/config'));
const CommonHelper = require(path.join(__dirname,"../helpers/commonHelpers"));
const fs = require('fs');

module.exports.studentList = function(req,res)
{
    //判断是否登录
    CommonHelper.isLogin(req,res);
    //当前页码值
    const page = parseInt(req.query.page || 1);

    //每页显示多少条
    const limit = 3;

    //偏移量
    const start = (page-1)*limit;

    let where = 1;

    db.table('student').where(where).count('*').then(function(count){

        //总页数
        const totalPage = count % limit == 0 ? count/limit : parseInt(count/limit)+1;

        const totalPageArray = [];

        //分页
        for(var i=1;i<=totalPage;i++)
        {
            totalPageArray.push(i);
        }

        //查询数据
        db.table('student').alias('student').join({
            table: 'school',
            join: 'left',
            as: 'school',
            on: ['school_id', 'school_id']
        }).join({
            table: 'grade',
            join: 'left',
            as: 'grade',
            on: ['grade_id', 'grade_id']

        }).join({
            table: 'class',
            join: 'left',
            as: 'class',
            on: ['class_id', 'class_id']

        }).join({
            table: 'user',
            join: 'left',
            as: 'user',
            on: ['user_id', 'user_id']

        }).where(where).order('student.stu_id DESC').limit(start,limit).select().then(function(student_list){

            let data = {
                array:student_list,
                count:count,
                totalPageArray:totalPageArray,
                page:page,
                totalPage:totalPage,
            };
            CommonHelper.renderFile(req,res,"studentList.html",data);
        });


    });

};

module.exports.studentEdit = function(req,res)
{
    let stu_id = req.query.stu_id;

    if(!stu_id)
    {
        //重定向
        res.redirect(`/student/studentList`);
        return false;
    }


    let where = 1;
    db.table('student').where(`stu_id = "${stu_id}"`).find().then(function(stuId){
        db.table('school').where(where).select().then(function(schoolId){
            db.table('class').where(where).select().then(function(classId){
                db.table('grade').where(where).select().then(function(gradeId){
                    db.table('user').where(where).select().then(function(userId){

                        let data = {
                            "student":stuId,
                            "school_list":schoolId,
                            "class_list":classId,
                            "grade_list":gradeId,
                            "user_list":userId
                        }
                        CommonHelper.renderFile(req,res,"studentEdit.html",data);
                    })
                })
            })
        })

    })

};

module.exports.studentEditForm = function(req,res)
{

    let stu_id = req.query.stu_id;

    if(!stu_id)
    {
        //重定向
        res.redirect(`/student/studentEdit`);
        return false;
    }

    db.table('student').where(`stu_id = ${stu_id}`).find().then(function(stuId){

        let data = {
            "school_id":req.body.school_id,
            "grade_id":req.body.grade_id,
            "class_id":req.body.class_id
        }
        db.table('student').where(`stu_id = ${stu_id}`).update(data).then(function(affectId){

            if(affectId)
            {
                //重定向
                res.redirect(`/student/studentList`);
                return false;
            }else{
                //重定向
                res.redirect(`/student/studentEdit?class_id=${stu_id}`);
                return false;
            }
        });

    });
};

module.exports.studentDelete = function(req,res)
{
    let delete_id = req.query.delete_id

    db.table('student').where(`stu_id = ${delete_id}`).delete().then(function(res_stu){
        if(res_stu)
        {
            res.redirect(`/student/studentList`);
            return false;
        }else{
           res.redirect(`/student/studentList`);
            return false;
        }
    })
}